package jfox.platform.datadict.dao;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.ejb.Local;
import javax.ejb.Stateless;
import javax.persistence.NamedNativeQueries;
import javax.persistence.NamedNativeQuery;

import jfox.platform.datadict.entity.DictItem;
import jfox.platform.infrastructure.SuperDAO;

/**
 * @author <a href="mailto:yang_y@sysnet.com.cn">Young Yang</a>
 */
@NamedNativeQueries(
        {
        @NamedNativeQuery(
                name = DictItemDAOBean.GET_DICT_ITEM,
                query = "SELECT * FROM T_DICT_ITEM WHERE ID=$ID",
                resultClass = DictItem.class
        ),
        @NamedNativeQuery(
                name = DictItemDAOBean.GET_DICT_ITEM_BY_STRING_VALUE,
                query = "SELECT * FROM T_DICT_ITEM WHERE CATEGORY_ID=$CATEGORY_ID AND STRING_VALUE=$STRING_VALUE",
                resultClass = DictItem.class
        ),
        @NamedNativeQuery(
                name = DictItemDAOBean.GET_DICT_ITEM_BY_VALUE,
                query = "SELECT * FROM T_DICT_ITEM WHERE CATEGORY_ID=$CATEGORY_ID AND VALUE=$VALUE",
                resultClass = DictItem.class
        ),
        @NamedNativeQuery(
                name = DictItemDAOBean.GET_DICT_ITEMS_BY_CATEGORY_ID,
                query = "SELECT * FROM T_DICT_ITEM WHERE CATEGORY_ID=$CATEGORY_ID",
                resultClass = DictItem.class
        )

                }
)
@Stateless
@Local
public class DictItemDAOBean extends SuperDAO implements DictItemDAO {

    public static final String GET_DICT_ITEM = "GET_DICT_ITEM";
    public static final String GET_DICT_ITEM_BY_STRING_VALUE = "GET_DICT_ITEM_BY_STRING_VALUE";
    public static final String GET_DICT_ITEM_BY_VALUE = "GET_DICT_ITEM_BY_VALUE";
    public static final String GET_DICT_ITEMS_BY_CATEGORY_ID = "GET_DICT_ITEM_BY_CATEGORY_ID";

    public DictItem getDictItem(long id) {
        return (DictItem)getEntityObjectByColumn(GET_DICT_ITEM, "ID", id);
    }

    public DictItem getDictItemByStringValue(long categoryId, String stringValue) {
        Map<String, Object> params = new HashMap<String, Object>(2);
        params.put("CATEGORY_ID", categoryId);
        params.put("STRING_VALUE", stringValue);
        return (DictItem)processNamedNativeQuery(GET_DICT_ITEM_BY_STRING_VALUE, params);
    }

    public DictItem getDictItemByValue(long categoryId, int value) {
        Map<String, Object> params = new HashMap<String, Object>(2);
        params.put("CATEGORY_ID", categoryId);
        params.put("VALUE", value);
        return (DictItem)processNamedNativeQuery(GET_DICT_ITEM_BY_VALUE, params);
    }

    public List<DictItem> getDictItemsByCategoryId(long categoryId) {
        Map<String, Object> params = new HashMap<String, Object>(2);
        params.put("CATEGORY_ID", categoryId);
        return (List<DictItem>)processNamedNativeQuery(GET_DICT_ITEMS_BY_CATEGORY_ID, params);
    }

    public static void main(String[] args) {

    }
}
